// FilerFrog.cpp : Implementation of DLL Exports.
#include "stdafx.h"
#include "resource.h"
#include "FilerFrog_i.h"
#include "dllmain.h"
#include "..\FilerFrogUtils\FilerFrogUtils.h"
#include "FilerFrogShellExt.h"

// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
    return _AtlModule.DllCanUnloadNow();
}


// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}

void AddRegisteryKey(const wchar_t* key)
{
	HKEY hkey;
	DWORD dwDisposition; 
	DWORD dwType, dwSize;

	long lResult = RegCreateKeyEx(
						HKEY_CLASSES_ROOT,
						key,
						0,
						NULL,
						REG_OPTION_NON_VOLATILE,
						KEY_ALL_ACCESS,
						NULL,
						&hkey,
						&dwDisposition);

	if (lResult == ERROR_SUCCESS)
	{
		wchar_t* szValue = L"{1C86B4A4-CA30-4A78-8167-C77E3B623DB0}";

		dwType = REG_SZ;
		dwSize = (DWORD)((lstrlen(szValue)+1)*sizeof(TCHAR));
		
		lResult = RegSetValueEx(hkey, NULL, 0, dwType, (PBYTE)szValue, dwSize);
		
		RegCloseKey(hkey);
	}
}

// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
	AddRegisteryKey(L"AllFilesystemObjects\\shellex\\ContextMenuHandlers\\FilerFrog");
	AddRegisteryKey(L"lnkfile\\shellex\\ContextMenuHandlers\\FilerFrog");
	AddRegisteryKey(L"Directory\\Background\\shellex\\ContextMenuHandlers\\FilerFrog");	
	
    // registers object, typelib and all interfaces in typelib
	HRESULT hr = _AtlModule.DllRegisterServer(TRUE);
	return hr;
}

void RemoveRegisteryKey(const wchar_t* key)
{
	HKEY hkey;

	long lResult = RegOpenKeyEx(
		HKEY_CLASSES_ROOT,
		key,	
		REG_OPTION_NON_VOLATILE,
		KEY_ALL_ACCESS,
		&hkey);

	if (lResult == ERROR_SUCCESS)
	{
		RegDeleteKey(hkey, L"");
		RegCloseKey(hkey);
	}
}

// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
	RemoveRegisteryKey(L"AllFilesystemObjects\\shellex\\ContextMenuHandlers\\FilerFrog");
	RemoveRegisteryKey(L"lnkfile\\shellex\\ContextMenuHandlers\\FilerFrog");	
	RemoveRegisteryKey(L"Directory\\Background\\shellex\\ContextMenuHandlers\\FilerFrog");	

	// Delete Sceret File
	// wstring seceretFilePath = FilerFrogUtils::GetSeceretFilePath();
	// DeleteFile(seceretFilePath.c_str());

	HRESULT hr = _AtlModule.DllUnregisterServer(TRUE);
	return hr;
}

// DllInstall - Adds/Removes entries to the system registry per user
//              per machine.	
STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
{
    HRESULT hr = E_FAIL;
    static const wchar_t szUserSwitch[] = _T("user");

    if (pszCmdLine != NULL)
    {
    	if (_wcsnicmp(pszCmdLine, szUserSwitch, _countof(szUserSwitch)) == 0)
    	{
    		AtlSetPerUserRegistration(true);
    	}
    }

    if (bInstall)
    {	
    	hr = DllRegisterServer();
    	if (FAILED(hr))
    	{	
    		DllUnregisterServer();
    	}
    }
    else
    {
    	hr = DllUnregisterServer();
    }

    return hr;
}


